import requests
import subprocess
import json
import re
import time
from hashlib import sha256

timer = int(time.time() * 1000)


def get_h5st(p):
    ret = subprocess.check_output(f"node ./h5st.js '{json.dumps(p)}'", shell=True).decode().strip()
    print(ret)
    return re.search(":::(?P<h5st>.*?):::", ret).group("h5st")


cookies = {
    "pinId": "lFj01T8WBFWWVE_D-pclSQ",
    "__jdu": "1890072925",
    "shshshfpa": "f71577ff-d1bb-6120-91ad-d87f51bed976-1710315526",
    "shshshfpx": "f71577ff-d1bb-6120-91ad-d87f51bed976-1710315526",
    "__jdv": "76161171|www.bing.com|-|referral|-|1731379737922",
    "PCSYCityID": "CN_810000_813000_0",
    "areaId": "52993",
    "ipLoc-djd": "52993-52994-0-0",
    "TrackID": "1daAP8WnW3qhTRFSo3RsrPmE9j6ROuD_ghCDuWzo2Hmc0qZ77wwmR5ozmrPlOJP3rlyoIOz99kfw3o4KU75pSuY6KCXABGPedxOi43VHGTFx7KM9-KCCSkmFzW4_z4uIr",
    "thor": "B42C935B1D8BF86ED6AE35A636A6604BE94D25C8901F0F7CBD1EC962E866772EAB3BCD208C5222C8604815005FBDFFFD9283CF90EAD674C675F4CECB6CC17C66F895AFDFD369F5E27A036DB6A24B97B06B9131ED3F3B5D469F6C6F9EF4D8B1EB089AFAA496DBCBE698392997E703A4A17BAEC9B3A3C4EB0962B5C68C5993CF2CA455F716622C36698890CE04AA196BADD66A569269C87B231B811A13BC5AA13B",
    "light_key": "AASBKE7rOxgWQziEhC_QY6yanjVZzXy0c8cLrxlAoobqU9h3CfIawHhYMecQ7y7r8VDekEtK",
    "pin": "jd_SPIWmKdqxdgV",
    "unick": "jd_u8cd6y36v1pq8u",
    "ceshi3.com": "000",
    "_tp": "83Q18zbA%2FPjgiuiRdkwLXQ%3D%3D",
    "_pst": "jd_SPIWmKdqxdgV",
    "umc_count": "1",
    "jsavif": "1",
    "__jda": "143920055.1890072925.1725501711.1725700937.1731379738.3",
    "__jdc": "143920055",
    "3AB9D23F7A4B3CSS": "jdd0336RUQFP7XTO6OKWRTJIBMBX27BH3E47P6ZZKXIGB7PELJFN77LQBJZGF6QEGIZ7TCAET2KXDEXSTGYXSRBJ5ZTPMGAAAAAMTDZPYYBAAAAAADECTQOSY4VGAUIX",
    "flash": "3_1Vo6inUMpIK82zU3VZZSo3xq7Ukk7e_r8BTAUzlIGRLlOQPtXbNBXE8opMfGQkIKg6WI-kF0C8XyA9r72mYhEjQ_rIEKyiKcOmW8APAgeJfhWCpPrFIICSCOSmUFL_V9U7SZscAAtxpBDXeKI_0ZoIBvNWZnRGEkYSrMoq322aKuOK5YyoT4",
    "__jdb": "143920055.9.1890072925|3.1731379738",
    "shshshfpb": "BApXSBspoHfZA49nvI_yPlCNksQjqeGisBlJwUglv9xJ1Mrtj7IO2",
    "3AB9D23F7A4B3C9B": "36RUQFP7XTO6OKWRTJIBMBX27BH3E47P6ZZKXIGB7PELJFN77LQBJZGF6QEGIZ7TCAET2KXDEXSTGYXSRBJ5ZTPMGA"
}

headers = {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "origin": "https://search.jd.com",
    "priority": "u=1, i",
    "referer": "https://search.jd.com/",
    "sec-ch-ua": "\"Chromium\";v=\"130\", \"Microsoft Edge\";v=\"130\", \"Not?A_Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0",
    "x-referer-page": "https://search.jd.com/Search",
    "x-rp-client": "h5_1.0.0"
}

params = {
    'appid': 'search-pc-java',
    'functionId': 'pc_search_s_new',
    'client': 'pc',
    'clientVersion': '1.0.0',
    't': str(timer),
    'body': '{"keyword":"电脑","pvid":"ad165442ea614f3c98c2a5487bb40d63","isList":0,"page":"16","s":"296","click":"0","log_id":"1725761333304.2552","show_items":""}',
    'loginType': '3',
    'uuid': '143920055.17042850208541662500281.1704285021.1725758684.1725761083.29',
    'area': '5_224_0_0',
    # 'h5st': '20240908100934415;99n99559igtm6y51;f06cc;tk03w88711b6318nHgiuuiDETIQbGq-OG8_Nc30eHnDfcQ52TRiHAd7Isp17RHnXjp9D5YT4chktBFNTv_BAn3bzKmPC;e189774d1448dfccf2a0f20682106d31;4.7;1725761374415;UOm4Xe0PHwPKHoQsqe0Tn7ejk4c_igL7OiSvMOk80bxKK_tm9syjLItqCVtzs2AJVRnIWQYQQ2N40ArkVk8lqcO-avn5j7V4eunop9U0dKEklrel1i1ZJnkSQIv0twQWbeuJ4abQqyP_YCtDeYj5pDt8xFgyZBqPa-bu2uJlDU5pqHa5tHynF64i9l1D6k7wzyvwu_0UKhB_4pPx-ZahQ36tbp0nwYOFoPDsfqMj8iTKhOOM_PqK_YS-YFjDyp7I_yil0ANsI2R0j1TQ6oXJy0mPfgF8NsFzUR4ADQZmklq4CEj7CDnosuc6XOJu_E3DD-teapUgNGnkxyzrlJnnzRFt07NX--jGh7cF_P870OPZHRBaORf0HbMdM7qpC8wWI-O-jKiNFAL9D1l95kDOMJ0TCZ3xS8pMQXtCCr4iebMNZ2IbfweVdTSPsf00gCPFUgJR-1wK0DjfaL4YrIEYeF3rRo9Hn2o9kCQ_wWEjfT9VtrmgdVkvOsMV6IeS2nEV-RNmDKGr4cSW4Jg1VwG2j9a-gaT-g0VDFwlLloQkC1l_fRdLc8zAwJ_uBT0-eqdxce60QCJ4pBu3aWHAKjuvyYyDwxL3E2UTOq5A8_dGSqEiSU1MrfZy--K3O56hREYguSt09mu3Rb6RtORJu8aBFo5n;7f590ba4a166b516c0fcefbd3749d63b',
    'x-api-eid-token': 'jdd03WX3GIVBDKCL2HIJPOZKJA5D2SZHZYWO5UVL4RB5OYS6GH5II6Q4OXME2TIUX5R5HPSEK7BHEELDJ77G7ULWZLIWHVUAAAAMRZ453YHQAAAAACRFMDMGIQLUP2MX',
}

s = sha256()
s.update(params["body"].encode())

body = s.hexdigest()

params2 = {
    "appid": "search-pc-java",
    "functionId": "pc_search_s_new",
    "client": "pc",
    "clientVersion": "1.0.0",
    "t": timer,
    "body": body
}

h5st = get_h5st(params2)
print("h5st", h5st)
params["h5st"] = h5st

response = requests.get('https://api.m.jd.com/', params=params, cookies=cookies, headers=headers)
print(":::", response.text)
